# -*- coding: latin-1 -*-
#
# Template library that exposes a 'etoiles' filter. Given an
# UVFiliere instance, it outputs the name of the specialization,
# prefixed by the number of stars it has in that particular
# department.

from django import template

register = template.Library()

def etoiles(uv_filiere):
    filiere = uv_filiere.filiere
    return ('%s<span class="filiere" title="%s (%s)">%s</span>'
            % ('*'*uv_filiere.nb_etoiles, filiere.nom,
               filiere.departement.code, filiere.code.replace('&', '&amp;')))

def note(note_pc, n_places=0):
    """Displays a mark (given as a percentage) in a vague,
    wetware-friendly way. n_places gives the number of decimal places
    to display."""
    if note_pc == -1:
        return 'n/a'
    format_str = '%%.%df/10' % n_places
    return format_str % round(note_pc/10.0, n_places)

register.filter('etoiles', etoiles)
register.filter('note', note)

def paginator(context, adjacent_pages=2):
    """
    To be used in conjunction with the object_list generic view.

    Adds pagination context variables for use in displaying first, adjacent and
    last page links in addition to those created by the object_list generic
    view.
    """
    page_numbers = [n for n in \
                    range(context["page"] - adjacent_pages, context["page"] + adjacent_pages + 1) \
                    if n > 0 and n <= context["pages"]]
    return {
        "hits": context["hits"],
        "results_per_page": context["results_per_page"],
        "page": context["page"],
        "pages": context["pages"],
        "page_numbers": page_numbers,
        "next": context["next"],
        "previous": context["previous"],
        "has_next": context["has_next"],
        "has_previous": context["has_previous"],
        "show_first": 1 not in page_numbers,
        "show_last": context["pages"] not in page_numbers,
    }

register.inclusion_tag("paginator.html", takes_context=True)(paginator)
